package org.daidai.abstrusewriting.homepage.live.tools;

/**
 * Created by Administrator on 2016/11/2.
 */

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;

import com.squareup.picasso.Transformation;

/**
 * Created by Administrator on 2016/10/15.
 */
/////////////////剪裁图片成圆形  写在Picasso  Picaasso.transform 中//////
public class CircleImage implements Transformation {
    @Override
    public Bitmap transform(Bitmap source) {
        Bitmap ret ;
        int sw = source.getWidth();
        int sh = source.getHeight();

        int bw = Math.min(sw , sh);

        int scx = sw >> 1;
        int hbw = bw >> 1;
        ret = Bitmap.createBitmap(source , scx - hbw , 0 , bw , bw);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        BitmapShader shader = new BitmapShader(
                source ,
                Shader.TileMode.CLAMP,
                Shader.TileMode.CLAMP
        );
        paint.setShader(shader);


        ret = Bitmap.createBitmap(bw , bw , Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(ret);

        int half = bw >> 1;
        canvas.drawCircle(half , half , half , paint);

        canvas = null;
        paint = null;

        //原始图像不用了
        source.recycle();

        return ret;
    }

    /**
     * 名称  唯一标识
     * @return
     */
    @Override
    public String key() {
        return null;
    }
}

